<?php

namespace services\oss;

use Yii;
use common\models\base\Project;
use common\models\patient\ClinicImg;
use common\components\Service;
use common\models\oss\ImgRecord;

/**
 * Class ImgRecordService
 * @package services\oss
 */
class ImgRecordService extends Service
{
    /**
     * 根据ID查找信息
     *
     * @param int $id
     * @return ImgRecord|null
     */
    public function findById(int $id = 0): ?ImgRecord
    {
        return ImgRecord::find()->where(['id' => $id])->one();
    }

    /**
     * 查找信息
     *
     * @param array $condition
     * @return ImgRecord|null
     */
    public function findInfo(array $condition = []): ?ImgRecord
    {
        $query = ImgRecord::find();

//        isset($condition['patient_user_id']) && $query->andWhere(['patient_user_id' => $condition['patient_user_id']]);

        return $query->one();
    }

    /**
     * 查找列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = ImgRecord::find();

        // condition
        isset($condition['id']) && $query->andWhere(['id' => $condition['id']]);
        isset($condition['img_name']) && $query->andWhere(['img_name' => $condition['img_name']]);

        // indexBy
        isset($condition['indexBy']) && $query->indexBy($condition['indexBy']);

        return $query->asArray()->all();
    }

    /**
     * 处理固定时间前的图片
     *
     * @param int $day
     * @return array|\yii\db\ActiveRecord[]
     */
    public function clearHistoryImage(int $day = 15)
    {
        //$imageRoot = env('FILE_UPLOAD_DIR') . '/';
        $imageRoot = Yii::$app->params['file_upload_dir'];
        $time = strtotime(date('Ymd', strtotime("-" . $day . "day")));
        $query = ImgRecord::find();
        $data = $query->where(['<=', 'updated_at', $time])->andWhere(['status' => ImgRecord::STATUS_ENABLE])->asArray()->all();
        foreach ($data as $list) {
            file_exists($imageRoot . $list['img_name']) && unlink($imageRoot . $list['img_name']);
        }

        $idData = array_column($data, 'id');
        ImgRecord::updateAll(['status' => ImgRecord::STATUS_DELETE], ['id' => $idData]);

        // 删除patient_clinic_img表对应的数据
        $patientClinicImgQuery = ClinicImg::find()->select(['id', 'project_id', 'img_url', 'thumb_img_url'])->where(['<=', 'updated_at', $time]);

        foreach ($patientClinicImgQuery->batch(500) as $tmpData) {
            foreach ($tmpData as $value) {
                $name_sx = Project::find()->select('name_sx')->where(['id' => $value['project_id']])->scalar();
                file_exists($imageRoot . '/' . $name_sx . '/' . $value['img_url']) && unlink($imageRoot . '/' . $name_sx . '/' . $value['img_url']);
                file_exists($imageRoot . '/' . $name_sx . '/' . $value['thumb_img_url']) && unlink($imageRoot . '/' . $name_sx . '/' . $value['thumb_img_url']);
            }
            // $tmpIdData = array_column($tmpData, 'id');
            // ClinicImg::deleteAll(['id' => $tmpIdData]);
        }

        return $data;
    }
}
